package com.jxdinfo.hussar.cloud.module.example.lift.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxdinfo.hussar.cloud.module.example.lift.dao.LiftDao;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.DeviceInfoDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftSearchDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftTodayRealDataDto;
import com.jxdinfo.hussar.cloud.module.example.lift.model.Lift;
import com.jxdinfo.hussar.cloud.module.example.lift.service.LiftService;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.sun.jndi.toolkit.dir.SearchFilter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 升降机表(Lift)表控制层
 *
 * @author makejava
 * @since 2023-10-23 17:34:00
 */
@RestController
@RequestMapping("/lift")
public class LiftController  {
    /**
     * 服务对象
     */
    @Resource
    private LiftService liftService;
    @Resource
    private LiftDao liftDao;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param lift 查询实体
     * @return 所有数据
     */
    @GetMapping
    public ApiResponse selectAll(Page<Lift> page, Lift lift) {
        return ApiResponse.success(this.liftService.page(page, new QueryWrapper<>(lift)));
    }
    /**
     * 查询升降机列表
     * @param liftSearchDto
     * @return
     */
    @PostMapping("/findByList")
    public ApiResponse<List<LiftDto>> findByList(@RequestBody LiftSearchDto liftSearchDto) {

        if(ObjectUtils.isNotEmpty(liftSearchDto.getTyProjectCode())){
            liftSearchDto.setTyProjectCode(liftSearchDto.getTyProjectCode());
        }else{
          //  liftSearchDto.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
        }

        List<Lift> list = liftService.findList(liftSearchDto);
        //List<LiftDto> result = this.convertToDtos(list);
        List<LiftDto> result = new ArrayList<>();
        return ApiResponse.success(result);
    }
    /**
     * 获取所有设备的告警情况-新
     * @param liftSearchDto
     * @return
     */
    @GetMapping("/findLiftAlarmCount")
    public ApiResponse<Map<String, Integer>> findLiftAlarmCount(LiftSearchDto liftSearchDto) {
        if (liftSearchDto == null){
            return null;
        }
        if(ObjectUtils.isNotEmpty(liftSearchDto.getTyProjectCode())){
            liftSearchDto.setTyProjectCode(liftSearchDto.getTyProjectCode());
        }else{
            //liftSearchDto.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
        }
        //liftSearchDto.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
        return ApiResponse.success(liftService.findLiftAlarmCount(liftSearchDto));
    }
    /**
     * 获取升降机实时数据
     * @param liftId
     * @return
     */
    @GetMapping("/findLiftRealData")
    public ApiResponse<LiftTodayRealDataDto> findLiftRealData(String liftId) {
        return ApiResponse.success(liftService.findLiftRealData(liftId, "ProjectUtil.getProjectInfo().getProjectCode())"));
    }
    /**
     * 分页查询升降机列表
     * @param liftSearchDto
     * @return
     */
    @PostMapping("/findByPage")
    public ApiResponse<Page<LiftDto>> findByPage(LiftSearchDto liftSearchDto) {

        if(ObjectUtils.isNotEmpty(liftSearchDto.getTyProjectCode())){
            liftSearchDto.setTyProjectCode(liftSearchDto.getTyProjectCode());
        }else{
          //  liftSearchDto.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
        }

        Page<Lift> pageResult = liftService.findPage(liftSearchDto);

      //  ResultData<PageResult<LiftDto>> result = this.convertToDtoPageResult(pageResult);

        return ApiResponse.success();
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ApiResponse selectOne(@PathVariable Serializable id) {
        return ApiResponse.success(this.liftService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param lift 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    public ApiResponse insert(@RequestBody LiftDto lift) {
      //  lift.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
        return liftService.insert(lift);

    }

    /**
     * 修改数据
     *
     * @param lift 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    public ApiResponse update(@RequestBody LiftDto lift) {
        //  lift.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
        return liftService.updateLift(lift);
    }

    /**
     * 删除数据
     * @return 删除结果
     */
    @PostMapping("/deleteById")
    public ApiResponse deleteById(@RequestBody LiftDto liftDto) {
        //  lift.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
        return liftService.deleteById(liftDto);
    }
    /**
     * 修改通知配置
     * @param liftDto
     * @return
     */
    @PostMapping("/updateNotice")
    public ApiResponse updateNotice(@RequestBody LiftDto liftDto) {
        try {
           // liftDto.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());

            return liftService.updateNotice(liftDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.fail(e.getMessage());
        }
    }
    /**
     * 根据设备类型获取升降机监测设备列表
     * @param liftSearchDto
     * @return
     */
    @GetMapping("/findDeviceByProductType")
    public ApiResponse<List<DeviceInfoDto>> findDeviceByProductType(LiftSearchDto liftSearchDto) {

       // liftSearchDto.setTyProjectCode(ProjectUtil.getProjectInfo().getProjectCode());
       // List<DeviceInfoDto> list = deviceService.findDeviceByProductType(DeviceTypeEnum.DEVICE_TYPE_LIFT, liftSearchDto.getTyProjectCode());
        LambdaQueryWrapper<Lift> wrapper = new LambdaQueryWrapper();
        wrapper.eq(ObjectUtils.isNotEmpty(liftSearchDto.getTyProjectCode()),Lift::getTyProjectCode,liftSearchDto.getTyProjectCode())
                .eq(ObjectUtils.isNotEmpty(liftSearchDto.getId()),Lift::getId,liftSearchDto.getId())
                .eq(Lift::getTyDeleteStatus,0);
        List<Lift> towers = liftService.list(wrapper);
        Set<String> deviceCodes = new HashSet<>();
        if(towers != null) {
            deviceCodes = towers.stream().map(Lift::getDeviceCode).collect(Collectors.toSet());
        }

        List<DeviceInfoDto> resultList = new ArrayList<>();

//        if(list != null) {
//            for (DeviceInfoDto deviceInfoDto : list) {
//                if(!deviceCodes.contains(deviceInfoDto.getDeviceCode())) {
//                    resultList.add(deviceInfoDto);
//                }
//            }
//        }

        return ApiResponse.success(resultList);
    }
}

